﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FormAdd_Mark : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader rd;
        public FormAdd_Mark()
        {
            InitializeComponent();
        }
        private bool checkexists()
        {
            bool check = false;
            string stucod = txtStuCode.Text;
            string subcod1 = cmSubject.SelectedValue.ToString();
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select * from TblStu_Sub where Student_Code='" + stucod + "' and Subject_Code ='" + subcod1 + "'";
            cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count>0)
            {
                check=true;
            }
            da.Dispose();
            conn.Close();
            return check;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtStuCode.Text == "" || txtTakTime.Text == "" || txtMark.Text == "")
            {
                MessageBox.Show("Please insert");
            }
            else
            {
                if (checkexists())
                {
                    MessageBox.Show("Subject's Mark of this Student is existed!");
                    cmSubject.Focus();
                }
                else
                {
                    conn = Connect.getConnection();
                    try
                    {
                        if (conn != null)
                        {
                            string subcod = cmSubject.SelectedValue.ToString();
                            conn.Open();
                            String strsql = "insert into TblStu_Sub values ('" + txtStuCode.Text + "','" + subcod + "','" + txtTakTime.Text + "','" + txtMark.Text + "')";
                            cmd = new SqlCommand(strsql, conn);
                            rd = cmd.ExecuteReader();
                            conn.Close();
                            MessageBox.Show("Add successful!");

                            txtMark.Text = "";
                            cmSubject.Focus();
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error", "Please try again");
                    }
                }
           }
    }

        private void btnExit_Click(object sender, EventArgs e)
        {
            FrmResult FrmResult = new FrmResult();
            FrmResult.Show();
            this.Hide();
        }
        private void LoadcmSubject()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select * from TblSubject";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            cmSubject.DataSource = dt;
            cmSubject.DisplayMember = "Subject_Name";
            cmSubject.ValueMember = "Subject_Code";

            
        }
        private void Loadfrmlist()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select TblStudents.Student_Code from TblStudents where Student_Code='" + FrmListStudent.studentcode + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtStuCode.Text = dr.GetString(0);
            }
            conn.Open();
        }
        private void loadfrmresult()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select TblStudents.Student_Code from TblStudents where Student_Code='" + FrmResult.Stu_Code1 + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtStuCode.Text = dr.GetString(0);
            }
            conn.Open();
        }
        private void FormAdd_Mark_Load(object sender, EventArgs e)
        {
            LoadcmSubject();
            Loadfrmlist();
            loadfrmresult();
            
            
        }
        private void btnBackResult_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    
 }
}